﻿using System;
using System.Collections.Generic;
using SARS.Biz;
using SARS.Biz.Facades;
using SARS.Entity;

namespace SARS.UserControl
{
    public partial class UcProgramCourse : System.Web.UI.UserControl
    {
        private readonly ICourseBO _courseBO = new CourseBO();
        private readonly IProgramBO _programmeBO = new ProgramBO();

        public delegate void ucProgramCourse_SeletedProgramChangHandler();
	    public delegate void ucProgramCourse_SeletedCourseChangHandler();

        public event ucProgramCourse_SeletedProgramChangHandler SelectedProgramChanged; 
	    public event ucProgramCourse_SeletedCourseChangHandler SelectedCourseChanged;

        public bool IsManual { get; set; }

	    public object ProgramDataSource
	    {
			get { return ddlPrograms.DataSource; }
			set 
            { 
                ddlPrograms.DataSource = value;
                ddlPrograms.DataBind();
            }
	    }

		public object CourseDataSource
		{
			get { return ddlCourses.DataSource; }
			set
			{
			    ddlCourses.DataSource = value;
                ddlCourses.DataBind();
			}
		}

        public int SelectedProgramValue
        {
            get 
            {
                int result;
                Int32.TryParse(ddlPrograms.SelectedValue, out result);
                return result;
            }
        }

        public int SelectedCourseValue
        {
            get
            {
                int result;
                Int32.TryParse(ddlCourses.SelectedValue,out result);
                return result;
            }
        }

        public bool IsCourseVisible
        {
            get { return ddlCourses.Visible; }
            set { ddlCourses.Visible = value; }
        }

        public bool IsCourseLabelVisible
        {
            get { return Label2.Visible; }
            set { Label2.Visible = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
			{
			    if (!IsManual)
			    {
                    ddlPrograms.DataSource = RetrieveProgramList();
                    ddlPrograms.DataTextField = "ProgramName";
                    ddlPrograms.DataValueField = "ProgramID";
                    ddlPrograms.DataBind();    
			    }
                
                ddlPrograms.Items.Insert(0,"- Please select the program -");
                ReloadCourseList();
            }
        }

        protected void ddlPrograms_OnSelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlPrograms.SelectedIndex == 0)
            {
                //if program is reset to zero then the course will be reset too
                ddlCourses.Items.Clear();
            }
            
            if (SelectedProgramChanged != null)
            {
                SelectedProgramChanged();
            }
            ReloadCourseList();
        }

		protected void ddlCourses_OnSelectedIndexChanged(object sender, EventArgs e)
		{
			if (SelectedCourseChanged != null)
			{
				SelectedCourseChanged();
			}
		}

        private void ReloadCourseList()
        {
            if (ddlPrograms.SelectedValue != null && ddlPrograms.SelectedIndex != 0)
            {
                if (!IsManual)
                {
                    ddlCourses.DataSource = _courseBO.RetriveCoursesByProgrammeID(Int32.Parse(ddlPrograms.SelectedValue));
                    ddlCourses.DataTextField = "CourseInfo";
                    ddlCourses.DataValueField = "CourseID";
                    ddlCourses.DataBind();
                }
                
                ddlCourses.Items.Insert(0, "- Please select the course -");
                
            }
        }

        private List<ProgramEntity> RetrieveProgramList()
        {
            return _programmeBO.RetrieveProgrammeAll();
        }
    }
}